package com.example.anjia03_3z.controller;


import com.example.anjia03_3z.pojo.dto.CategoryAddNewDTO;
import com.example.anjia03_3z.pojo.dto.CategoryUpdateDTO;
import com.example.anjia03_3z.pojo.vo.CategoryListItemVO;
import com.example.anjia03_3z.pojo.vo.CategoryOptionsVO;
import com.example.anjia03_3z.pojo.vo.CategoryStandardVO;
import com.example.anjia03_3z.service.ICategoryService;
import com.example.anjia03_3z.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "01.品类管理")
@Validated
public class CategoryController {

    @Autowired
    ICategoryService service;

    @PostMapping("/add-new")
    @ApiOperation("添加品类")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        service.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> delete(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id) {
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
//    多个不冲突的正则表达式可以共存 有占位符与无占位符可以共存 无占位符但满足正则表达式会优先使用较为精准的方法
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> update(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id,
            @Valid
                    CategoryUpdateDTO categoryUpdateDTO) {
        service.updateById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用分类")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "分类编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> setEnable(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id) {
        service.setEnable(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用分类")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "分类编号", dataType = "Long", required = true)
    })
    public JsonResult<Void> disable(
            @Range(min = 1, message = "数据范围异常")
            @PathVariable
                    Long id) {
        service.disable(id);
        return JsonResult.ok();
    }


    @GetMapping("/getOptions")
    @ApiOperation("获取分类选项")
    @ApiOperationSupport(order = 401)
    public JsonResult<List<CategoryOptionsVO>> getOptions(){
        List<CategoryOptionsVO> options = service.getOptions();
        return JsonResult.ok(options);
    }

    @GetMapping("/{parentId:[0-9]+}/list-by-parentId")
    @ApiOperation("查询品类列表")
    @ApiOperationSupport(order = 402)
    public JsonResult<List<CategoryListItemVO>> listByParentId(
            @Range(min = 0, message = "数据范围异常")
            @PathVariable
            Long parentId){
        List<CategoryListItemVO> voList = service.listByParentId(parentId);
        return JsonResult.ok(voList);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询品类")
    @ApiOperationSupport(order = 403)
    public JsonResult<CategoryStandardVO> getStandard(
            @Range(min = 0, message = "数据范围异常")
            @PathVariable
                    Long id){
        CategoryStandardVO standard = service.getStandard(id);
        return JsonResult.ok(standard);
    }

    @RequestMapping("/upload-icon")
    @ApiOperation("上传图片")
    @ApiOperationSupport(order = 500)
    public JsonResult<String> uploadIcon(MultipartFile picFile){
        String iconPath = service.uploadIcon(picFile);
        return JsonResult.ok(iconPath);
    }

    @GetMapping("/remove")
    @ApiOperation("删除图片图片")
    @ApiOperationSupport(order = 501)
    public JsonResult<Void> remove(String url){
        service.remove(url);
        return JsonResult.ok();
    }



}
